const express = require('express');
const validate = require('../../middleware/validate');
const { auth, authorize } = require('../../middleware/auth'); // 假设的认证和授权中间件
const dietRecordValidation = require('../../validators/dietRecord.validator');
const dietRecordController = require('../../controllers/dietRecord.controller');

const router = express.Router();

// swaggerDef.js 或类似文件中需要定义 DietRecord 相关的schemas和responses

/**
 * @swagger
 * tags:
 *   name: DietRecords
 *   description: 用户饮食记录管理 (管理员接口)
 */

router
  .route('/')
  /**
   * @swagger
   * /admin/diet-records:
   *   post:
   *     summary: 创建新的饮食记录 (管理员可以为指定用户创建)
   *     tags: [DietRecords]
   *     security:
   *       - bearerAuth: []
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/DietRecordCreate' # 需要在swaggerDef中定义
   *     responses:
   *       "201":
   *         description: 创建成功
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/DietRecord'
   *       "400":
   *         $ref: '#/components/responses/BadRequest'
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .post(
    auth(),
    authorize(['manageUsers', 'manageContent']), // 假设需要管理用户或内容的权限
    validate(dietRecordValidation.createDietRecord),
    dietRecordController.createDietRecord
  )
  /**
   * @swagger
   * /admin/diet-records:
   *   get:
   *     summary: 获取饮食记录列表 (管理员可以查看所有或筛选)
   *     tags: [DietRecords]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: query
   *         name: userId
   *         schema:
   *           type: string
   *         description: 用户ID
   *       - in: query
   *         name: mealType
   *         schema:
   *           type: string
   *           enum: [早餐, 午餐, 晚餐, 加餐]
   *         description: 餐类型
   *       - in: query
   *         name: startDate
   *         schema:
   *           type: string
   *           format: date
   *         description: 开始日期 (YYYY-MM-DD)
   *       - in: query
   *         name: endDate
   *         schema:
   *           type: string
   *           format: date
   *         description: 结束日期 (YYYY-MM-DD)
   *       - in: query
   *         name: minTotalCalories
   *         schema:
   *           type: integer
   *         description: 最小总热量
   *       - in: query
   *         name: maxTotalCalories
   *         schema:
   *           type: integer
   *         description: 最大总热量
   *       - in: query
   *         name: search
   *         schema:
   *           type: string
   *         description: 搜索关键字 (食物名称, 备注等)
   *       - in: query
   *         name: sortBy
   *         schema:
   *           type: string
   *         description: 排序字段 (e.g., date:desc)
   *       - in: query
   *         name: limit
   *         schema:
   *           type: integer
   *           minimum: 1
   *         default: 10
   *         description: 每页数量
   *       - in: query
   *         name: page
   *         schema:
   *           type: integer
   *           minimum: 1
   *         default: 1
   *         description: 页码
   *     responses:
   *       "200":
   *         description: OK
   *         content:
   *           application/json:
   *             schema:
   *               type: object
   *               properties:
   *                 results: 
   *                   type: array
   *                   items: 
   *                     $ref: '#/components/schemas/DietRecord'
   *                 page: { type: integer }
   *                 limit: { type: integer }
   *                 totalPages: { type: integer }
   *                 totalResults: { type: integer }
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .get(
    auth(), 
    authorize(['manageUsers', 'manageContent']), 
    validate(dietRecordValidation.getDietRecords),
    dietRecordController.getDietRecords
  );

/**
 * @swagger
 * /admin/diet-records/summary:
 *   get:
 *     summary: 获取饮食记录统计摘要
 *     tags: [DietRecords]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: userId
 *         schema:
 *           type: string
 *         description: 用户ID (可选，不提供则统计全部用户)
 *       - in: query
 *         name: date
 *         schema:
 *           type: string
 *           format: date
 *         description: 指定日期 (YYYY-MM-DD)，默认为今天
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [day, week, month]
 *         default: week
 *         description: 统计周期 (日/周/月)
 *     responses:
 *       "200":
 *         description: 获取统计数据成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: number
 *                   example: 0
 *                 message:
 *                   type: string
 *                   example: "获取饮食记录摘要成功"
 *                 body:
 *                   type: object
 *                   properties:
 *                     userId:
 *                       type: string
 *                       description: 用户ID (如果指定了用户)
 *                     period:
 *                       type: string
 *                       description: 统计周期
 *                     startDate:
 *                       type: string
 *                       format: date-time
 *                       description: 统计开始日期
 *                     endDate:
 *                       type: string
 *                       format: date-time
 *                       description: 统计结束日期
 *                     totalRecords:
 *                       type: number
 *                       description: 记录总数
 *                     totalCalories:
 *                       type: number
 *                       description: 总热量摄入
 *                     averageDailyCalories:
 *                       type: number
 *                       description: 日均热量摄入
 *                     nutritionTotal:
 *                       type: object
 *                       properties:
 *                         protein: { type: number }
 *                         fat: { type: number }
 *                         carbohydrate: { type: number }
 *                       description: 营养素总摄入
 *                     nutritionAverage:
 *                       type: object
 *                       properties:
 *                         protein: { type: number }
 *                         fat: { type: number }
 *                         carbohydrate: { type: number }
 *                       description: 营养素日均摄入
 *                     mealTypeSummary:
 *                       type: object
 *                       properties:
 *                         早餐: 
 *                           type: object
 *                           properties:
 *                             count: { type: number }
 *                             totalCalories: { type: number }
 *                             averageCalories: { type: number }
 *                         午餐: 
 *                           type: object
 *                           properties:
 *                             count: { type: number }
 *                             totalCalories: { type: number }
 *                             averageCalories: { type: number }
 *                         晚餐: 
 *                           type: object
 *                           properties:
 *                             count: { type: number }
 *                             totalCalories: { type: number }
 *                             averageCalories: { type: number }
 *                         加餐: 
 *                           type: object
 *                           properties:
 *                             count: { type: number }
 *                             totalCalories: { type: number }
 *                             averageCalories: { type: number }
 *                       description: 各餐类型统计
 *                     dailySummary:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           date: { type: string, format: date }
 *                           totalCalories: { type: number }
 *                           nutrition: 
 *                             type: object
 *                             properties:
 *                               protein: { type: number }
 *                               fat: { type: number }
 *                               carbohydrate: { type: number }
 *                       description: 每日统计数据
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 */
router.route('/summary')
  .get(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(dietRecordValidation.getDietRecordSummary),
    dietRecordController.getDietRecordSummary
  );

// 注意：/:id 路由必须放在 /summary 路由之后，否则 'summary' 会被当作 id
router
  .route('/:id')
  /**
   * @swagger
   * /admin/diet-records/{id}:
   *   get:
   *     summary: 获取单条饮食记录详情
   *     tags: [DietRecords]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: 饮食记录ID
   *     responses:
   *       "200":
   *         description: OK
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         $ref: '#/components/responses/NotFound'
   */
  .get(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(dietRecordValidation.getDietRecord),
    dietRecordController.getDietRecord
  )
  /**
   * @swagger
   * /admin/diet-records/{id}:
   *   put:
   *     summary: 更新饮食记录
   *     tags: [DietRecords]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: 饮食记录ID
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/DietRecordUpdate' # 需要在swaggerDef中定义
   *     responses:
   *       "200":
   *         description: 更新成功
   *       "400":
   *         $ref: '#/components/responses/BadRequest'
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         $ref: '#/components/responses/NotFound'
   */
  .put(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(dietRecordValidation.updateDietRecord),
    dietRecordController.updateDietRecord
  )
  /**
   * @swagger
   * /admin/diet-records/{id}:
   *   delete:
   *     summary: 删除饮食记录 (逻辑删除)
   *     tags: [DietRecords]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: 饮食记录ID
   *     responses:
   *       "200":
   *         description: 删除成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         $ref: '#/components/responses/NotFound'
   */
  .delete(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(dietRecordValidation.deleteDietRecord),
    dietRecordController.deleteDietRecord
  );

module.exports = router; 